{# SPDX-License-Identifier: Apache-2.0 -#}

{% extends "admin/base.html" %}

{% macro render_item(item) -%}
  {% if item is mapping %}
    <ul class="list-unstyled">
      {% for key, value in item.items() %}
      <li><em>{{ key }}</em>: {{ render_item(value) }}</li>
      {% endfor %}
    </ul>
  {% elif item is sequence and item is not string %}
    <ul>
      {% for subitem in item %}
      <li>{{ render_item(subitem) }}</li>
      {% endfor %}
    </ul>
  {% else %}
    {{ item }}
  {% endif %}
{%- endmacro %}

{% macro render_data(data) -%}
  <dl class="dl-horizontal">
    {% for name, value in data.items() %}
    <dt>{{ name }}</dt>

    {# Normalize everything to a list, containing at least one item #}
    {% if value is not sequence or value is string %}
      {% set value = [value] %}
    {% endif %}

    {% for item in value %}
    <dd>
      {{ render_item(item) }}
    </dd>
    {% endfor %}

    {% endfor %}
  </dl>
{%- endmacro %}

{% macro status_color(status) -%}
  {%- if status.value == "Delivered" -%}
    green
  {%- elif status.value == "Soft Bounced" -%}
    yellow
  {%- elif status.value == "Bounced" -%}
    red
  {%- elif status.value == "Complained" -%}
    red
  {%- else -%}
    muted
  {%- endif -%}
{%- endmacro %}

{% block title %}Message Details{% endblock %}

{% block breadcrumb %}
  <li class="breadcrumb-item"><a href="{{ request.route_path('admin.emails.list') }}">Emails</a></li>
  <li class="breadcrumb-item active">Message Details</li>
{% endblock %}


{% block content %}
  <div class="row">
    <div class="col-md-12">
      <div class="card">
        <div class="card-header">
          <h3 class="card-title">
            <i class="fa fa-envelope-open"></i>
            Message
          </h3>
        </div>

        <div class="card-body">
          <dl class="dl-horizontal">
            <dt>Message Id</dt>
            <dd>{{ email.message_id }}</dd>

            <dt>From</dt>
            <dd>{{ email.from_ }}</dd>

            <dt>To</dt>
            <dd>
              {{ email.to }}
              <a href="{{ request.route_path('admin.user.list', _query={'q': 'email:' + email.to}) }}">
                <i class="fa-solid fa-magnifying-glass"></i>
              </a>
            </dd>

            <dt>Subject</dt>
            <dd>{{ email.subject }}</dd>

            {% if email.missing %}
            <dt>Missing</dt>
            <dd>"{{ email.to }}" was missing from the database during processing.</dd>
            {% endif %}

            <dt>Status</dt>
            <dd class="text-{{ status_color(email.status) }}">{{ email.status.value }}</dd>
          </dl>
        </div>
      </div>
    </div>
  </div>

  <div class="row">
    <div class="col-md-12">
      <div class="card">
        <div class="card-header">

          <h3 class="card-title">
            <i class="fa fa-exchange-alt"></i>
            Events
          </h3>
        </div>

        <div class="card-body">
          <div class="timeline timeline-inverse">
            {% for event in email.events|reverse %}
            <div>
              {# Modify the icon and color based on the event type #}
              {% if event.event_type.value == "Delivery" %}
                {% set event_icon = "envelope" %}
                {% set event_color = "green" %}
              {% elif event.event_type.value == "Bounce" %}
                {% set event_icon = "exclamation" %}

                {% if event.data.bounceType == "Permanent" %}
                  {% set event_color = "red" %}
                {% else %}
                  {% set event_color = "orange" %}
                {% endif %}
              {% elif event.event_type.value == "Complaint" %}
                {% set event_icon = "ban" %}
                {% set event_color = "red" %}
              {% else %}
                {% set event_icon = "asterisk" %}
                {% set event_color = "blue" %}
              {% endif %}

              <i class="fa fa-{{ event_icon }} bg-{{ event_color }}"></i>
              <div class="timeline-item">
                <span class="time"><i class="fa fa-clock"></i> {{ event.created|format_datetime }}</span>
                <h3 class="timeline-header">{{ event.event_type.value }}</h3>
                <div class="timeline-body">
                  {{ render_data(event.data) }}
                </div>
              </div>
            </div>
            {% endfor %}

            <div>
              <i class="fa fa-check bg-blue"></i>

              <div class="timeline-item">
                <span class="time"><i class="fa fa-clock"></i> {{ email.created|format_datetime }}</span>
                <h3 class="timeline-header no-border">Accepted</h3>
              </div>
            </div>
            <div>
              <i class="fas fa-clock bg-gray"></i>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
{% endblock %}
